package phone.qyg.com.qyg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.ApplicationConfig;
import com.app.utils.CleanMessageUtil;
import com.app.utils.CommonUtil;
import com.app.utils.SharePrefUtil;
import com.app.utils.TransformController;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.MessageFormat;

import phone.qyg.com.qyg.R;

/**
 * 作者：lblbh on 2017/12/4 09:39
 * 邮箱：lanbuhan@163.com
 * 功能：设置
 */
@ContentView(R.layout.setting)
public class SettingActivity extends BaseActivity {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.update_password)
    TextView update_password;

    @ViewInject(R.id.cache_vaule)
    TextView cache_vaule;

    @ViewInject(R.id.quit_login)
    TextView quit_login;

    @ViewInject(R.id.app_info)
    TextView app_info;

    @ViewInject(R.id.app_video)
    TextView app_video;

    @ViewInject(R.id.app_update)
    TextView app_update;

    @ViewInject(R.id.app_version)
    TextView app_version;

    @ViewInject(R.id.clear_app)
    RelativeLayout clear_app;

    @ViewInject(R.id.app_update_rela)
    RelativeLayout app_update_rela;

    String userInfo;

    @Override
    public void initData(Bundle savedInstanceState) {
        //设置应用缓冲大小
//        File externalCacheDir = this.getExternalCacheDir();
//        String formatSize = CommonUtil.getFormatSize(CommonUtil.gileSize(externalCacheDir));

        cache_vaule.setText(CleanMessageUtil.getTotalCacheSize(QXApplication.getContext()));

        userInfo = SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER, "");
        quit_login.setVisibility((isRequestStr(userInfo))? View.VISIBLE:View.GONE);
        update_password.setVisibility((isRequestStr(userInfo))? View.VISIBLE:View.GONE);

        boolean appInfoSwitch= SharePrefUtil.getBoolean(SharePrefUtil.KEY.APP_INFO_SWITCH,false);
        boolean appVideoSwitch= SharePrefUtil.getBoolean(SharePrefUtil.KEY.APP_VIDEO_SWITCH,false);
        boolean appUpdateSwitch= SharePrefUtil.getBoolean(SharePrefUtil.KEY.APP_UPDATE_SWITCH,false);
        app_info.setSelected(appInfoSwitch);
        app_video.setSelected(appVideoSwitch);
        app_update.setSelected(appUpdateSwitch);

        app_version.setText(MessageFormat.format(QXApplication.getContext().getResources().getString(R.string.app_version_key), CommonUtil.getVersion()));
    }

    @Event(value = {R.id.back,R.id.update_password,R.id.clear_app,R.id.app_about,R.id.quit_login,
            R.id.app_info,R.id.app_video,R.id.app_update,R.id.app_update_rela})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.update_password:
                TransformController.transformController(this,UpdatePasswordActivity.class);
                break;
            case R.id.app_about:

                break;
            case R.id.clear_app:
//                File externalCacheDir = this.getExternalCacheDir();
//                boolean delete = CommonUtil.deleteFileSafely(externalCacheDir);
//                DataCleanManager.cleanExternalCache(QXApplication.getContext());
//                if(delete){
//                    cache_vaule.setText("0.0");
//                }else{
//                    Intent in = new Intent();
//                    in.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
//                    in.setData(Uri.parse("package:"+getPackageName()));
//                    in.putExtra("cmp", "com.android.settings/.applications.InstalledAppDetails");
//                    in.addCategory("android.intent.category.DEFAULT");
//                    startActivity(in);
//                }

                CleanMessageUtil.clearAllCache(QXApplication.getContext());
                cache_vaule.setText("0.0");
                break;
            case R.id.quit_login:
                SharePrefUtil.clearLogingSaveData();
                Intent intent = new Intent(ApplicationConfig.APP_QX_LOGIN);
                intent.putExtra(TransformController.ChangeKEY.INTENT_KEY,"");
                LocalBroadcastManager.getInstance(QXApplication.getContext()).sendBroadcast(intent);
                SharePrefUtil.saveString(SharePrefUtil.KEY.APP_USER,"");
                this.finish();
                break;
            case R.id.app_info:
                //接收系统消息
                boolean appInfoSwitch= SharePrefUtil.getBoolean(SharePrefUtil.KEY.APP_INFO_SWITCH,false);
                boolean isAppInfo = !appInfoSwitch;
                app_info.setSelected(isAppInfo);
                SharePrefUtil.saveBoolean(SharePrefUtil.KEY.APP_INFO_SWITCH,isAppInfo);
                break;
            case R.id.app_video:
                boolean appVideoSwitch= SharePrefUtil.getBoolean(SharePrefUtil.KEY.APP_VIDEO_SWITCH,false);
                boolean isAppVideo = !appVideoSwitch;
                app_video.setSelected(isAppVideo);
                SharePrefUtil.saveBoolean(SharePrefUtil.KEY.APP_VIDEO_SWITCH,isAppVideo);
                break;
            case R.id.app_update:
                boolean appUpdateSwitch= SharePrefUtil.getBoolean(SharePrefUtil.KEY.APP_UPDATE_SWITCH,false);
                boolean isAppUpdate = !appUpdateSwitch;
                app_update.setSelected(isAppUpdate);
                SharePrefUtil.saveBoolean(SharePrefUtil.KEY.APP_UPDATE_SWITCH,isAppUpdate);
                break;
            case R.id.app_update_rela:

                break;
        }
    }

    @Override
    public void initSucessView() {}
    @Override
    public void initViewHandler() {}
}
